import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import java.sql.PreparedStatement;

/*
JDBC连接Mysql
PreparedStatement语句
预编译
 */

public class JDBCPreparedStatementDemo {
    private static final String URL = "jdbc:mysql://localhost:3306/jdbcDemo";
    private static final String USERNAME = "root";
    private static final String PWD = "123456";

    public static void update() {// 增删改
        Connection connection = null;
        PreparedStatement  pstmt = null;
        try {
            // a.导入驱动，加载具体的驱动类
            Class.forName("com.mysql.jdbc.Driver");
            // b.与数据库建立连接
            connection = DriverManager.getConnection(URL, USERNAME, PWD);

            //PreparedStatement
            String sql = "insert into `13` values('2','sd')";
            pstmt = connection.prepareStatement(sql);//预编译
            pstmt.setInt('1', 36);
            pstmt.setString('2', "zhans");

            int count =pstmt.executeUpdate() ;

            // d.处理结果
            if (count > 0) {
                System.out.println("操作成功！");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(pstmt!=null) pstmt.close();// 对象.方法
                if(connection!=null)connection.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static void query() {
        Connection connection = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null ;
        try {
            // a.导入驱动，加载具体的驱动类
            Class.forName("com.mysql.jdbc.Driver");
            // b.与数据库建立连接
            connection = DriverManager.getConnection(URL, USERNAME, PWD);
            // c.发送sql，执行(增删改、【查】)
            Scanner input= new Scanner(System.in);
            System.out.println("请输入id：");
            int id = input.nextInt() ;

            //预编译
//			String sql = "select * from student where stuname like '%"+name+"%'";
//			String sql = "select * from student where stuname like ?";
            String sql ="select count(*) from `13` where `id`= ? ";
            pstmt = connection.prepareStatement(sql);

            pstmt.setInt(1, id);

            // 执行SQL(增删改executeUpdate()，查询executeQuery())
            rs = pstmt.executeQuery();
            // d.处理结果
//			while(rs.next()) {
//				int rsid = rs.getInt( "id") ;
//				String rsmane = rs.getString("name") ;
////				int sno = rs.getInt(1) ; //下标：从1开始计数
////				String sname = rs.getString(2) ;
//				System.out.println(id+"--"+name);
//			}
            int count = -1;
            if(rs.next()) {
                count = rs.getInt(1) ;
            }
            if(count>0) {
                System.out.println("登陆成功！");
            }else {
                System.out.println("登陆失败！");
            }

        } catch (ClassNotFoundException e) {
            System.out.println("111");
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch(Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(rs!=null) rs.close();
                if(pstmt!=null) pstmt.close();// 对象.方法
                if(connection!=null)connection.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }catch(Exception e) {
                e.printStackTrace();
            }
        }
    }


    public static void main(String[] args) {
//		update() ;
        query() ;
    }
}
